#!/usr/bin/python3.8
# -*- coding: utf-8 -*-
# @Time    : 2021/3/1 16:00
# @Author  : zhaohexin
# @Email   : 1483643520@xxx.com
# @File    : model.py
# @Software: PyCharm
# @FileDescription：用户表模型类

from datetime import datetime

from sqlalchemy import Column, VARCHAR, Integer, Boolean, DateTime, BigInteger

from DatabaseOperation.BaseModel import BaseModel, BaseNullModel


class User(BaseModel):
    """
    用户表
    """
    __tablename__ = "sys_user"
    __table_args__ = ({"comment": "系统用户表"})
    dept_id = Column(Integer, nullable=True, default="", comment="部门ID")
    username = Column(VARCHAR(255), nullable=False, comment="用户名称")
    nick_name = Column(VARCHAR(255), nullable=False, comment="用户昵称")
    gender = Column(VARCHAR(2), nullable=True, default="未知", comment="性别")
    phone = Column(VARCHAR(255), nullable=True, default="", comment="联系电话")
    email = Column(VARCHAR(255), nullable=True, default="", comment="邮箱")
    avatar_name = Column(VARCHAR(255), nullable=True, default="", comment="头像地址")
    avatar_path = Column(VARCHAR(255), nullable=True, default="", comment="头像真实地址")
    password = Column(VARCHAR(255), nullable=False, comment="密码")
    is_admin = Column(Boolean, nullable=True, default=False, comment="是否为管理账号")
    enabled = Column(Boolean, nullable=True, default=False, comment="是否启用")
    pwd_reset_time = Column(DateTime, nullable=True, default=datetime.now().strftime('%Y-%m-%d %H:%M:%S'),
                            comment="密码更新时间")

    # 把SQLAlchemy查询对象转换成字典
    def to_dict(self):
        return {
            "avatarName": self.avatar_name,
            "avatarPath": self.avatar_path,
            "createTime": self.create_time.strftime('%Y-%m-%d %H:%M:%S'),
            "dept_id": self.dept_id,
            "email": self.email,
            "enabled": self.enabled,
            "gender": self.gender,
            "id": self.id,
            "nickName": self.nick_name,
            "phone": self.phone,
            "pwdResetTime": self.pwd_reset_time.strftime('%Y-%m-%d %H:%M:%S'),
            "updateTime": self.update_time.strftime('%Y-%m-%d %H:%M:%S'),
            "updateBy": self.update_by,
            "createBy": self.create_by,
            "username": self.username
        }


class UsersRoles(BaseNullModel):
    """
    角色用户关联表
    """
    __tablename__ = "sys_users_roles"
    __table_args__ = ({"comment": "角色用户关联表"})
    user_id = Column(BigInteger, primary_key=True, comment="用户ID")
    role_id = Column(BigInteger, primary_key=True, comment="角色ID")

    # 把SQLAlchemy查询对象转换成字典
    def to_dict(self):
        return {c.name: getattr(self, c.name) for c in self.__table__.columns}


class UserJobs(BaseNullModel):
    """
    岗位用户关联表
    """
    __tablename__ = "sys_users_jobs"
    __table_args__ = ({"comment": "岗位用户关联表"})

    user_id = Column(BigInteger, primary_key=True, comment="用户ID")
    job_id = Column(BigInteger, primary_key=True, comment="岗位ID")

    # 把SQLAlchemy查询对象转换成字典
    def to_dict(self):
        return {c.name: getattr(self, c.name) for c in self.__table__.columns}
